capture log close
log using $log/AED_dataprep_01_outcome_weekly, text replace
   
clear
set mem 6000m
set matsize 800
set more off
set maxvar 5000
set trace off
   
/* --------------------------------------------------------- */
/* --- Generation weekly outcome variables ----------------- */
/* ----------------------------------------------------------*/


local iebversion v901
  

forvalues year= 1996/1998 {
	
		use $orig/AED_9310_v1, clear
		
		qui drop if endorig<mdy(1,1,`year')
		qui drop if begorig>mdy(12,31,`year')
	
		keep persnr begorig endorig erwstat tentgelt quelle betnr stib
		rename persnr iza_id
		rename begorig beg_orig
		rename endorig end_orig
		rename erwstat erw_stat
		rename tentgelt tag_entg
		rename betnr bet_nr
		rename stib berufstellg
		sort iza_id beg_orig 
		
		
		foreach month in 01 02 03 04 05 06 07 08 09 10 11 12 {
		
			forvalue week = 1/4 {
	
			di "### `year' `month' `week'"
			di c(current_time)
			if "`month'"=="01" { 
				local m "Jan"
			}	
			if "`month'"=="02" { 
				local m "Feb"
			}	
			if "`month'"=="03" { 
				local m "Mar"
			}	
			if "`month'"=="04" { 
				local m "Apr"
			}	
			if "`month'"=="05" { 
				local m "May"
			}	
			if "`month'"=="06" { 
				local m "Jun"
			}	
			if "`month'"=="07" { 
				local m "Jul"
			}	
			if "`month'"=="08" { 
				local m "Aug"
			}	
			if "`month'"=="09" { 
				local m "Sep"
			}	
			if "`month'"=="10" { 
				local m "Oct"
			}	
			if "`month'"=="11" { 
				local m "Nov"
			}
			if "`month'"=="12" { 
				local m "Dec"
			}

			
			if `week' == 1 {
				local d 5
			}
			if `week' == 2 {
				local d 12
			}
			if `week' == 3 {
				local d 19
			}
			if `week' == 4 {
				local d 26
			}
			
			
			#delimit ;
			/*BEH, LEH and ASU Information*/
			qui by iza_id: gen iza_beh_`year'`month'`week'_0 = erw_stat  if (beg_orig<=d(`d'`m'`year')
			& end_orig>=d(`d'`m'`year'))
			& quelle==1;
			qui by iza_id: gen iza_leh_`year'`month'`week'_0 = erw_stat  if (beg_orig<=d(`d'`m'`year')
			& end_orig>=d(`d'`m'`year'))
			& quelle==2;
			qui by iza_id: gen iza_asu_`year'`month'`week'_0 = erw_stat  if (beg_orig<=d(`d'`m'`year')
			& end_orig>=d(`d'`m'`year'))
			& (quelle==7 | quelle==18);
			qui by iza_id: gen iza_lhg_`year'`month'`week'_0 = erw_stat  if (beg_orig<=d(`d'`m'`year')
			& end_orig>=d(`d'`m'`year'))
			& (quelle==16 | quelle==19);
						
			/*Daily earnings*/
			qui by iza_id: gen iza_geld_beh_`year'`month'`week'_0 = tag_entg  if (beg_orig<=d(`d'`m'`year')
			& end_orig>=d(`d'`m'`year'))
			& quelle==1;
			qui by iza_id: gen iza_geld_leh_`year'`month'`week'_0 = tag_entg  if (beg_orig<=d(`d'`m'`year')
			& end_orig>=d(`d'`m'`year'))
			& quelle==2;
			
			/*Establishment identifier*/
			#delimit ;
			qui by iza_id: gen iza_bet_`year'`month'`week'_0 = bet_nr if (beg_orig<=d(`d'`m'`year')
			& end_orig>=d(`d'`m'`year'));
			#delimit cr
				
			/*Occupational status and working hours*/
			#delimit ;
			qui by iza_id: gen iza_azeit_`year'`month'`week'_0 = berufstellg if (beg_orig<=d(`d'`m'`year')
			& end_orig>=d(`d'`m'`year'));
			#delimit cr
				
			/*Extending info for all observations per unit*/
			/*Depending employment domintes secondary employment*/
			qui by iza_id: egen    iza_beh_`year'`month'`week' = min(iza_beh_`year'`month'`week'_0)
			drop iza_beh_`year'`month'`week'_0			
			qui by iza_id: egen    iza_leh_`year'`month'`week' = min(iza_leh_`year'`month'`week'_0)
			drop iza_leh_`year'`month'`week'_0			
			qui by iza_id: egen    iza_asu_`year'`month'`week' = min(iza_asu_`year'`month'`week'_0)
			drop iza_asu_`year'`month'`week'_0	
			qui by iza_id: egen    iza_lhg_`year'`month'`week' = min(iza_lhg_`year'`month'`week'_0)
			drop iza_lhg_`year'`month'`week'_0	
			
			/*Main income dominates secondary earnings*/
			qui by iza_id: egen    iza_geld_beh_`year'`month'`week' = max(iza_geld_beh_`year'`month'`week'_0)
			drop iza_geld_beh_`year'`month'`week'_0			
			qui by iza_id: egen    iza_geld_leh_`year'`month'`week' = max(iza_geld_leh_`year'`month'`week'_0)
			drop iza_geld_leh_`year'`month'`week'_0			
						
			qui by iza_id: gen     iza_bet_`year'`month'`week'_temp = iza_bet_`year'`month'`week'_0
			qui by iza_id: replace iza_bet_`year'`month'`week'_temp = iza_bet_`year'`month'`week'_temp[_n-1] if iza_bet_`year'`month'`week'_0==.
			qui by iza_id: gen     iza_bet_`year'`month'`week'      = iza_bet_`year'`month'`week'_temp[_N]
			drop iza_bet_`year'`month'`week'_temp iza_bet_`year'`month'`week'_0			
			
			qui by iza_id: gen     iza_azeit_`year'`month'`week'_temp = iza_azeit_`year'`month'`week'_0
			qui by iza_id: replace iza_azeit_`year'`month'`week'_temp = iza_azeit_`year'`month'`week'_temp[_n-1] if iza_azeit_`year'`month'`week'_0==.
			qui by iza_id: gen     iza_azeit_`year'`month'`week'      = iza_azeit_`year'`month'`week'_temp[_N]
			drop iza_azeit_`year'`month'`week'_temp iza_azeit_`year'`month'`week'_0			
			
			}/*end:week*/
			} /* End: m (month) */
			
	qui bys iza_id: drop if _n>1
	/* Reduce Data */
	
	keep iza*
	sort iza_id
	
	compress
	save $data/outcome_weekly_y`year'_`iebversion', replace
	
	
} /* End: year */
	

log close			


